<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期" prop="dateInfo">
        <el-date-picker clearable
          v-model="queryParams.dateInfo"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataInfoList">
      <el-table-column label="日期" align="center" prop="dateInfo" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateInfo, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="机构ID" align="center" prop="infoId" />
      <el-table-column label="机构名称" align="center" prop="infoName" />
      <el-table-column label="撞库数量" align="center" prop="matchNumber" />
      <el-table-column label="url数量" align="center" prop="urlNumber" />
      <el-table-column label="展示数量" align="center" prop="showNumber" />
      <el-table-column label="点击数量" align="center" prop="clickNumber" />
      <el-table-column label="成功数量" align="center" prop="successNumber" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listDataInfo, getDataInfo, delDataInfo, addDataInfo, updateDataInfo } from "@/api/merchant/dataInfo";

export default {
  name: "DataInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户数据统计表格数据
      dataInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: null,
        infoId: null,
        infoName: null,
        matchNumber: null,
        urlNumber: null,
        showNumber: null,
        clickNumber: null,
        successNumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dateInfo: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        infoId: [
          { required: true, message: "机构ID不能为空", trigger: "blur" }
        ],
        infoName: [
          { required: true, message: "机构名称不能为空", trigger: "blur" }
        ],
        matchNumber: [
          { required: true, message: "撞库数量不能为空", trigger: "blur" }
        ],
        urlNumber: [
          { required: true, message: "url数量不能为空", trigger: "blur" }
        ],
        showNumber: [
          { required: true, message: "展示数量不能为空", trigger: "blur" }
        ],
        clickNumber: [
          { required: true, message: "点击数量不能为空", trigger: "blur" }
        ],
        successNumber: [
          { required: true, message: "成功数量不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    $route() {
      this.getList()
    }
  },
  methods: {
    /** 查询商户数据统计列表 */
    getList() {
      this.loading = true;
      this.queryParams.infoId = this.$route.query.infoId;
      listDataInfo(this.queryParams).then(response => {
        this.dataInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dateInfo: null,
        infoId: null,
        infoName: null,
        matchNumber: null,
        urlNumber: null,
        showNumber: null,
        clickNumber: null,
        successNumber: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户数据统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDataInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商户数据统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDataInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDataInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商户数据统计编号为"' + ids + '"的数据项？').then(function() {
        return delDataInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('merchant/dataInfo/export', {
        ...this.queryParams
      }, `dataInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
